package com.luo.controller;

import com.luo.dao.impl.GoodMappersImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：
 * 作者：Luo。
 * 日期：2024/2/27 19:49
 */
@WebServlet("/addImgs")
@MultipartConfig
public class AddShopImgs extends HttpServlet {

    GoodMappersImpl goodMappers = new GoodMappersImpl();

    @Override
    protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        if (ServletFileUpload.isMultipartContent(request)) {

            try {
                // 获取webapp目录的绝对路径
                String webAppPath = getServletContext().getRealPath("/");

                DiskFileItemFactory factory = new DiskFileItemFactory();

                File uploadDir = new File(webAppPath + File.separator + "imgs");
                factory.setRepository(uploadDir);

                ServletFileUpload upload = new ServletFileUpload(factory);

                List<FileItem> fileItems = upload.parseRequest(request);
                String optionImg = null;
                String fileName = null;
                String id = null;
                for (FileItem fileItem : fileItems) {
                    if (!fileItem.isFormField()) {
                        fileName = new File(fileItem.getName()).getName();
                        String filePath = uploadDir + File.separator + fileName;
                        fileItem.write(new File(filePath));
                    }
                    if (fileItem.isFormField() && fileItem.getFieldName().equals("optionImg")) {
                        optionImg = fileItem.getString();
                        if ("true".equals(optionImg)) {
                            optionImg = "true";
                        } else if ("false".equals(optionImg)) {
                            optionImg = "false";
                        }
                    }
                    if (fileItem.isFormField() && fileItem.getFieldName().equals("id")) {
                        id = fileItem.getString();
                    }
                }
                if (id == null || fileName == null || optionImg == null) {
                    response.getWriter().write("-1");
                    return;
                }
                Map map = new HashMap<>();
                map.put("id",id);
                map.put("path","http://localhost:8080/mall_war_exploded/imgs/" + fileName);
                if ("true".equals(optionImg)) {
                    // 添加首页图片
                    goodMappers.addMainpicturesImgs(map);
                } else {
                    // 添加描述图片
                    goodMappers.addIntroducepictureImgs(map);
                }
                response.getWriter().write("1");
            } catch (FileUploadException e) {
                e.printStackTrace();
                response.getWriter().write("File upload failed.");
            } catch (Exception e) {
                e.printStackTrace();
                response.getWriter().write("Exception occurred: " + e.getMessage());
            }
        } else {
            response.getWriter().write("Request does not contain any file.");
        }
    }
}
